{layout name="layout1" /}
<style>
    .layui-table-cell {
        height: auto;
        white-space: normal;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
                <div class="layui-colla-item">
                    <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
                    <div class="layui-colla-content layui-show">
                        <p>*会员列表，对会员进行编辑，账户调整，等级调整和资料查看等操作。</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">会员信息：</label>
                    <div class="layui-input-inline" style="width: 200px;">
                        <select name="keyword_type" id="keyword_type">
                            <option value="sn">会员编号</option>
                            <option value="nickname">会员昵称</option>
                            <option value="mobile">会员手机号码</option>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="width: 200px;">
                        <input type="text" id="keyword" name="keyword" placeholder="请输入关键词" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <!--                <div class="layui-inline">-->
                <!--                    <label class="layui-form-label">会员等级：</label>-->
                <!--                    <div class="layui-input-inline">-->
                <!--                        <select id="level" name="level"   style="height:80px;width: 80px" >-->
                <!--                            <option value="" selected>全部</option>-->
                <!--                            <option value="0">无等级</option>-->
                <!--                            {foreach $level_list as $item }-->
                <!--                            <option value="{$item.id}">{$item.name}</option>-->
                <!--                            {/foreach}-->
                <!--                        </select>-->
                <!--                    </div>-->
                <!--                </div>-->
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">会员分组：</label>-->
<!--                    <div class="layui-input-inline">-->
<!--                        <select id="group_id" name="group_id" style="height:80px;width: 80px">-->
<!--                            <option value="">全部</option>-->
<!--                            {foreach $group_list as $item }-->
<!--                            <option value="{$item.id}">{$item.name}</option>-->
<!--                            {/foreach}-->

<!--                        </select>-->
<!--                    </div>-->
<!--                </div>-->

            </div>
            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class="layui-form-label">消费金额：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="total_amount_start" autocomplete="off">
                    </div>
                    <div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
                        <label class="layui-form-mid">-</label>
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="total_amount_end" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">注册时间:</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="start_time" name="start_time" autocomplete="off">
                    </div>
                    <div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
                        <label class="layui-form-mid">-</label>
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" id="end_time" name="end_time" autocomplete="off">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" lay-submit
                            lay-filter="user-search">查询
                    </button>
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit
                            lay-filter="user-clear-search">清空查询
                    </button>
                    <button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit
                            lay-filter="export-file">导出
                    </button>
                </div>
            </div>

        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-sm  layuiadmin-btn-user {$view_theme_color}" data-type="add">
                    新增用户
                </button>
                <!--                <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="send_coupon">发放优惠券</button>-->
                <button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="set_group">
                    设置分组
                </button>
            </div>

            <table id="user-lists" lay-filter="user-lists"></table>
            <script type="text/html" id="service_type">
                {{#  if(d.service_type == 1){ }}
                <span class="layui-badge layui-bg-blue">个人</span>
                {{#  } else if(d.service_type == 2){ }}
                <span class="layui-badge layui-bg-green">企业</span>
                {{#  } else { }}
                <span class="layui-badge layui-bg-gray">其他</span>
                {{#  } }}
            </script>
            <script type="text/html" id="avatar-info">
                <img src="{{d.avatar}}" style="height:40px;width: 40px;margin-right: 10px;" class="image-show"></img>
            </script>
            <script type="text/html" id="status-info">
                {{#  if(d.disable == 0){ }}
                <span class="layui-badge-dot layui-bg-blue"></span>正常
                {{#  } else { }}
                <span class="layui-badge-dot layui-bg-gray"></span>禁用
                {{#  } }}
            </script>
            <script type="text/html" id="approval_status">
                {{# if(d.service_info != '') {}}
                {{#  if(d.approval_status == 0){ }}
            <span class="layui-badge layui-bg-orange">待审批</span>
            {{#  } else if(d.approval_status == 1){ }}
            <span class="layui-badge layui-bg-green">已通过</span>
            {{#  } else if(d.approval_status == 2){ }}
            <span class="layui-badge layui-bg-gray">已拒绝</span>
            {{#  } else { }}
            <span class="layui-badge layui-bg-gray">未知</span>
            {{#  } }}
                {{# } else { }}
                <span class="layui-badge layui-bg-gray">无信息</span>
                {{# } }}
            </script>
            <script type="text/html" id="user-operation">
                <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="info">资料</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm" id="adjust_user" lay-event="adjust_user">账户调整</a>
                {{#  if(d.service_info != '' && d.approval_status == 0){ }}
                <a class="layui-btn layui-btn-normal layui-btn-sm"  lay-event="approval">审批</a>
                {{#  } else if(d.service_info != '' && d.approval_status == 1){ }}
                <a class="layui-btn layui-btn-normal layui-btn-sm" >已审批</a>
                {{#  } else if(d.service_info != '' && d.approval_status == 2){ }}
                <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="approval">审批不通过</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-normal layui-btn-sm" >无信息</a>
                {{#  } }}
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="quality_deposit">质保金调整</a>
            </script>
        </div>
    </div>
</div>
<script>
    layui.config({
        version: "{$front_version}",
        base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'like', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , like = layui.like
            , laydate = layui.laydate;

        //监听搜索
        form.on('submit(user-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('user-lists', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //日期时间范围
        laydate.render({
            elem: '#start_time'
            , type: 'datetime'
            , trigger: 'click'
        });

        //日期时间范围
        laydate.render({
            elem: '#end_time'
            , type: 'datetime'
            , trigger: 'click'
        });


        //清空查询
        form.on('submit(user-clear-search)', function () {
            $('#keyword_type').val('sn');
            $('#keyword').val('');  //清空输入框
            $('#level').val('');  //清空输入框
            $('#group_id').val('');  //清空输入框
            $('#start_time').val('');  //清空输入框
            $('#end_time').val('');  //清空输入框
            form.render('select');
            //刷新列表
            table.reload('user-lists', {
                where: [],
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 导出
        form.on('submit(export-file)', function (data) {
            var field = data.field;
            $.ajax({
                url: '{:url("user/exportFile")}',
                type: 'get',
                data: field,
                dataType: 'json',
                error: function () {
                    layer.msg('导出超时，请稍后再试!');
                },
                success: function (res) {
                    table.exportFile(res.data.exportTitle, res.data.exportData, res.data.exportExt, res.data.exportName);
                },
                timeout: 15000
            });
            layer.msg('导出中请耐心等待~');
        });


        //事件
        var active = {
            add: function () {
                var add_page = layer.open({
                    type: 2
                    , title: '新增用户'
                    , content: '{:url("user/add")}'
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'add-user-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field;
                            like.ajax({
                                url: '{:url("user/add")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    }
                                },
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
            , set_group: function () { //设置分组
                var check_status = table.checkStatus('user-lists')
                    , user_list = check_status.data; //得到选中的数据


                //是否已选数据
                if (user_list.length === 0) {
                    return layer.msg('请选择用户');
                }
                //获取所选id
                ids = [];
                for (var i in user_list) {
                    ids.push(user_list[i]['id']);
                }

                layer.open({
                    type: 2
                    , title: '设置分组'
                    , content: '{:url("user/setGroup")}'
                    , area: ['60%', '60%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'user-submit-edit'
                            , submit = layero.find('iframe').contents().find("#set_group-submit");
                        //监听提交
                        iframeWindow.layui.form.on('submit(set_group-submit)', function (data) {
                            var group_id = data.field.group_id;
                            $.ajax({
                                url: '{:url("user/setGroup")}',
                                data: {group_id: group_id, user_ids: ids},
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });

            }
        }
        $('.layui-btn.layuiadmin-btn-user').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(document).on('click', '.image-show', function () {
            var src = $(this).attr('src');
            like.showImg(src, 600);
        });
    });


    layui.define(['table', 'form'], function (exports) {
        var $ = layui.$
            , table = layui.table
            , form = layui.form;

        //管理员管理
        table.render({
            id: 'user-lists'
            , elem: '#user-lists'
            , url: '{:url("user/serviceLists")}' //模拟接口
            , cols: [[
                {type: 'checkbox'}
                , {field: 'id', width: 80, title: 'ID', sort: true, hide: true}
                , {field: 'service_type', title: '类型', align: "center", width: 100, templet: '#service_type'}
                // , {field: 'sn', title: '用户编号', align: "center", width: 120}
                , {field: 'account', title: '账号', align: "center", width: 120}
                , {field: 'nickname', title: '用户昵称', align: "center", width: 120}
                , {field: 'avatar', title: '头像', align: "center", width: 120, templet: '#avatar-info'}
                , {field: 'mobile', title: '手机号', align: "center", width: 120}
                , {field: 'approval_status', width: 120, title: '审批状态', templet: '#approval_status',align:'center', sort: true}
                // ,{field: 'user_growth', title: '成长值',align:"center",width: 100}
                , {field: 'user_money', title: '用户余额',  align: 'center', width:120}
                , {field: 'total_order_amount', align: "center", title: '消费金额', width: 90}
                , {field: 'quality_deposit_rate', align: "center", title: '质保金比例(%)', width: 130}
                // ,{title: '上级推荐人',align: 'center',width: 380,toolbar: '#first-info'}
                // ,{field: 'fans', title: '推荐下级人数',align:"center",width: 120}
                , {field: 'disable', align: "center", title: '状态', width: 80, templet: '#status-info'}
                , {field: 'login_time', align: "center", title: '最后登录时间', width: 180}
                , {field: 'create_time', align: "center", title: '注册时间', width: 180}
                , {fixed: 'right', title: '操作', width: 380, toolbar: '#user-operation'}

            ]]
            , page: true
            , text: {none: '暂无数据！'}
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.count, //解析数据长度
                    "data": res.data.lists, //解析数据列表
                };
            }
            , done: function () {
                // 解决操作栏因为内容过多换行问题
                $(".layui-table-main tr").each(function (index, val) {
                    $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                    $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                });
            }
        });

        var moreShow = 0;
        //监听工具条
        table.on('tool(user-lists)', function (obj) {
            if (obj.event === 'edit') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '编辑会员'
                    , content: '{:url("user/edit")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#edit-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(edit-submit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/edit")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })

            }
            if (obj.event === 'info') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '会员资料'
                    , content: '{:url("user/info")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['返回']
                })
            }
            if(obj.event === 'approval')
            {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '信息审批'
                    , content: '{:url("user/approval")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#approval-submit');
                            //监听提交
                        iframeWindow.layui.form.on('submit(approval-submit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/approval")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
            if(obj.event === 'quality_deposit')
            {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '质保金调整'
                    , content: '{:url("user/qualityDeposit")}?id=' + id
                    , area: ['90%', '50%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#quality-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(quality-submit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/qualityDeposit")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
            if (obj.event === 'adjust_user') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '账户调整'
                    , content: '{:url("user/adjustAccount")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#adjust_user-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(adjust_user-submit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/adjustAccount")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
            if (obj.event === 'adjust_level') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '等级调整'
                    , content: '{:url("user/adjustLevel")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#adjust_level-submit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(adjust_level-submit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/adjustLevel")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
            if (obj.event === 'adjust_first_leader') {
                var id = obj.data.id;
                layer.open({
                    type: 2
                    , title: '推荐人调整'
                    , content: '{:url("user/adjustFirstLeader")}?id=' + id
                    , area: ['90%', '90%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find('#formSubmit');
                        //监听提交
                        iframeWindow.layui.form.on('submit(formSubmit)', function (data) {
                            var field = data.field;
                            $.ajax({
                                url: '{:url("user/adjustFirstLeader")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user-lists'); //数据刷新
                                    } else {
                                        layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                        });
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                })
            }
        });
    });
</script>